﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewViews.Models;
using System.Data.Entity;
using System.Web.DataAccess;
using System.Web.Security;
//using NewViews.Models.User;
namespace NewViews.Controllers
{
    public class AccountController : Controller
    {
        EShopDataEntities5 dbEntities = new EShopDataEntities5();
       
        public IAccountRepository ar;
        public AccountController(IAccountRepository ar)
        {
            this.ar = ar;
        }
        public AccountController()
        {
        }
        //
        // GET: /Account/
        //UserModelDataContext context = new UserModelDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Logout()
        {
            
            ShoppingCart obj = new ShoppingCart();
            obj.EmptyCart();
            //Session.RemoveAll();
           // Session.Abandon();
            Session.Remove("key");
            Session.Remove("CartId");
            return RedirectToAction("index","Home");
        }
        public ActionResult SignUp()
        {
            return View();
        }
        public ActionResult AddUser(User u)
        {
           
            User new_user = new User();
            new_user.First_Name = Request["First_Name"];
            new_user.Last_Name = Request["Last_Name"];
            new_user.Email = Request["Email"];
            new_user. Password= Request["Password"];
           
            new_user.Address = Request["Address"];

            
            dbEntities.Users.Add(new_user);
            dbEntities.SaveChanges();
           
            return RedirectToAction("Index", "Home");
            //return View();
        }
        public JsonResult CheckUserName()
        {
            string email = Request["Email"];

            var UserLogin = dbEntities.Users;
            List<User> list = new List<User>();
            var users_dataList = UserLogin.ToList();
            foreach (var v in users_dataList)
            {
               if (v.Email.Equals(email))
                {
                    return this.Json(false, JsonRequestBehavior.AllowGet);
                }
            }
            return this.Json(true, JsonRequestBehavior.AllowGet);
        }
        public ActionResult VerifyLogin()
        {


            var admin_db = dbEntities.Admins;
            string Email = Request["email"];
            string Password = Request["password"];
            var Admin_dataList = dbEntities.Admins.ToList();
            

            foreach (var v in Admin_dataList)
            {
                if (v.Email.Equals(Email) && v.Password.Equals(Password))
                {
                    Session.Add("key", v.Email);
                    return RedirectToAction("AdminHome", "Admin");
                }
            }
            var Users_dataList = dbEntities.Users.ToList();
            var user_data = dbEntities.Users;
           
            foreach (var v in user_data)
            {
                if (v.Email.Equals(Email) && v.Password.Equals(Password))
                {
                    Session.Add("key", v.Email);
                    
                    return RedirectToAction("Index", "User");
                }
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult Login()
        {
            return View();
        }
    }
}
